package com.wanmait.recycle.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.recycle.pojo.Admin;

@WebFilter("/manager/*")
public class ManagerLoginFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		String path = req.getContextPath();
		HttpSession session = req.getSession();
		Admin admin = (Admin)session.getAttribute("admin");
		
		/*String sign = (String)session.getAttribute("sign");
		if(sign!=null) {
			chain.doFilter(request, response);
			return;
		}
		else {
			session.setAttribute("sign", "sign");
		}*/
		if(!req.getServletPath().equals("/manager/login")&&!req.getServletPath().equals("/manager/loginSubmit")&&!req.getServletPath().equals("/manager/checkCode")&&!req.getServletPath().equals("/manager/checkCodeIsTrue")) {
			
			if(admin==null) {
				res.sendRedirect(path+"/manager/login");
				return;
				
			}else {
				
			chain.doFilter(request, response);
			return;
			}
		}
		chain.doFilter(request, response);
	}

}
